<template>
  <div class="login-container">
    <el-card class="login-form">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">{{$t('login.userlogin')}}
            <el-dropdown trigger="click" style="float:right;color:grey;fontSize:25px " @command="handleCommand">
              <span class="el-dropdown-link">
                <svg-icon icon-class="language" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="zh">中文</el-dropdown-item>
                <el-dropdown-item command="en">英文</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>

          </h3>

        </div>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username" v-model="loginForm.username" placeholder="Username" name="username" type="text" />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="Password" name="password" @keyup.enter.native="handleLogin" />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">{{ $t('login.login') }}</el-button>
      </el-form>
    </el-card>
    <div class="back_img">
      <img src="./assets/logingBg.png" alt="">
    </div>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
// import { login } from '@/api/user'
import crypto from 'crypto'
// import { log } from 'util'

export default {
    name: 'Login',
    data() {
        const isEmail = (rule, value, callback) => {
            if (!value) {
                callback()
            } else {
                const reg =
                    /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
                const email = reg.test(value)
                if (!email) {
                    callback(new Error('邮箱格式如:admin@163.com'))
                } else {
                    callback()
                }
            }
        }

        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(
                    new Error('The password can not be less than 6 digits')
                )
            } else {
                callback()
            }
        }
        return {
            loginForm: {
                username: 'root@admin.com',
                password: '123456'
            },
            loginRules: {
                username: [
                    { required: true, trigger: 'blur', validator: isEmail }
                ],
                password: [
                    {
                        required: true,
                        trigger: 'blur',
                        validator: validatePassword
                    }
                ]
            },
            loading: false,
            passwordType: 'password',
            redirect: undefined
        }
    },
    watch: {
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        handleCommand(command) {
            console.log(command)
            this.$i18n.locale = command
        },
        // MD5加密
        setMd5() {
            var md5 = crypto.createHash('md5')
            md5.update(this.loginForm.password) // this.pw2这是你要加密的密码
            console.log(this.loginForm, 'this.loginForm-------')
            return md5.digest('hex') // this.pw这就是你加密完的密码，这个往后台传就行了
        },
        // sha256加密
        setSha() {
            const sha256 = require('js-sha256').sha256 // 这里用的是require方法，所以没用import
            // 这就是你加密之后的密码
            return sha256(this.loginForm.password) // 要加密的密码
        },

        showPwd() {
            if (this.passwordType === 'password') {
                this.passwordType = ''
            } else {
                this.passwordType = 'password'
            }
            this.$nextTick(() => {
                this.$refs.password.focus()
            })
        },
        handleLogin() {
            this.$refs.loginForm.validate(async(valid) => {
                if (valid) {
                    const password = this.setSha()
                    console.log(password)
                    await this.$store.dispatch('user/login', {
                        username: this.loginForm.username,
                        password
                    })

                    this.$router.push('/')
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        }
    }
}
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #000;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
        color: $cursor;
    }
}

/* reset element-ui css */
.login-container {
    width: 100%;
    height: 100%;
    background: url('./assets/bg.jpg') no-repeat center/ cover;
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;

        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: $light_gray;
            height: 47px;
            caret-color: $cursor;

            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px $bg inset !important;
                -webkit-text-fill-color: $cursor !important;
            }
        }
    }

    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
    // .el-form-item{
    //   background-color: #fff;
    // }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.el-form-item {
    margin-top: 30px;
}
.el-button {
    height: 50px;
    font-size: 25px;
}
.login-container {
    min-height: 100%;
    width: 100%;
    overflow: hidden;
    position: relative;
    .back_img {
        position: absolute;
        right: 10%;
        top: 25%;
        width: 520px;
        height: 350px;
        img {
            width: 520px;
            height: 350px;
        }
        // background: url('./assets/logingBg.png') no-repeat center / cover;
    }
    .login-form {
        position: absolute;
        left: 20%;
        top: 25%;
        width: 520px;
        max-width: 100%;
        background-color: #f5f5f5;
        margin: 0 auto;
        overflow: hidden;
        z-index: 1;
    }

    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;

        span {
            &:first-of-type {
                margin-right: 16px;
            }
        }
    }

    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 22px;
            color: #000;
            margin: 20px auto 10px auto;
            padding-left: 50px;
            background: url('./assets/loginLogo.png') no-repeat;
            font-weight: 500;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }
}
</style>
